#!/bin/bash

IP=`hostname -I | awk '{print $1}'`
SUBNET=`ip route | grep $IP | grep -v default | awk '{print $1}'`
RANGE1=${IP%.*}".10"
RANGE2=${IP%.*}".240"
ROUTERS=`ip route | awk 'NR==1 {print $3}'`

setenforce 0
systemctl --now disable firewalld

yum -y install epel-release

yum -y install tftp-server kea syslinux-nonlinux

cd /etc/kea
mv kea-dhcp4.conf kea-dhcp4.conf.back

cat <<EOF > kea-dhcp4.conf
{
        "Dhcp4": {
                "interfaces-config": {
                        "interfaces": ["*"]
                },

                        "lease-database": {
                                "type": "memfile",
                                "persist": true,
                                "name": "/var/lib/kea/dhcp4.leases"
                        },
                        "subnet4": [
                        {
                                "id": 1,
                                "subnet": "$SUBNET",
                                "pools": [
                                {
                                        "pool": "$RANGE1-$RANGE2"
                                }
                                ],
                                "option-data": [
                                {
                                        "name": "routers",
                                        "data": "$ROUTERS"
                                },
                                {
                                        "name": "domain-name-servers",
                                        "data": "119.29.29.29, 223.5.5.5"
                                },
                                {
                                        "name": "boot-file-name",
                                        "data": "pxelinux.0"
                                },
                                {
                                        "code": 66,
                                        "data": "$IP"
                                },
                                ],
                                "renew-timer": 1800,
                                "rebind-timer": 2700,
                                "valid-lifetime": 3600
                        },
                 ],
                "loggers": [
                {
                        "name": "kea-dhcp4",
                        "output-options": [
                        {
                                "output": "kea-dhcp4.log"
                        }
                        ],
                        "severity": "INFO",

                        "debuglevel": 0
                }
                ]
        }
}

EOF

cp /usr/share/syslinux/{ldlinux.c32,libcom32.c32,libutil.c32,vesamenu.c32,pxelinux.0} /var/lib/tftpboot/

for ver in {9..10}-stream
do
        for file in {initrd.img,vmlinuz}
        do
                curl --url "https://mirrors.ustc.edu.cn/centos-stream/$ver/BaseOS/x86_64/os/images/pxeboot/$file" -O --output-dir /var/lib/tftpboot/$ver --create-dirs
        done
done

chmod 755 /var/lib/tftpboot/{9..10}-stream

cd  /var/lib/tftpboot/
mkdir pxelinux.cfg
cd pxelinux.cfg
cat <<EOF > default
default vesamenu.c32
timeout 100
menu title PXE Auto Install Centos Stream

label el9
  menu default
  menu label ^Auto Install Centos Stream 9
  kernel 9-stream/vmlinuz
  append initrd=9-stream/initrd.img inst.repo=https://mirrors.ustc.edu.cn/centos-stream/9-stream/BaseOS/x86_64/os/ inst.ks=https://gitee.com/hgthyl/ks/raw/master/ks9.cfg

label el10
  menu label ^Auto Install Centos Stream 10
  kernel 10-stream/vmlinuz
  append initrd=10-stream/initrd.img inst.repo=https://mirrors.ustc.edu.cn/centos-stream/10-stream/BaseOS/x86_64/os/ inst.ks=https://gitee.com/hgthyl/ks/raw/master/ks10.cfg

label local
  menu label Boot from ^local drive
  localboot 0xffff

EOF

systemctl --now enable tftp kea-dhcp4
